//
//  LBADViewController.m
//  LvoeBeen_Imitation
//
//  Created by apple on 17/2/18.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LBADViewController.h"
#import "LBTabBarController.h"
#import <Masonry.h>

#define imgCnt 4
#define screenSize [UIScreen mainScreen].bounds,size
#define screenSizeW [UIScreen mainScreen].bounds.size.width
#define screenSizeH [UIScreen mainScreen].bounds.size.height

@interface LBADViewController ()<UIScrollViewDelegate>

@property (nonatomic,weak)UIPageControl *pageCtr;

@end

@implementation LBADViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    //添加scrollView
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.contentSize = CGSizeMake(screenSizeW * imgCnt, screenSizeH);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    //布局
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    //添加图片
    for (int i = 0; i < imgCnt; i++) {
        //添加imgView
        NSString *imgName = [NSString stringWithFormat:@"guide_40_%d",i + 1];
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
        imgView.userInteractionEnabled = YES;
        [scrollView addSubview:imgView];
        //布局
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(screenSizeW);
            make.height.mas_equalTo(screenSizeH);
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(scrollView).mas_offset(screenSizeW * i);
        }];
        //如果是最后一页
        if (i == imgCnt - 1) {
            //创建按钮
            UIButton *runBtn = [[UIButton alloc] init];
            [imgView addSubview:runBtn];
            [runBtn setImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
            //按钮布局
            [runBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(imgView);
                make.bottom.mas_equalTo(imgView).mas_offset(-100);
            }];
            //添加点击事件
            [runBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    //页面控制器
    UIPageControl *pageCtr = [UIPageControl new];
    pageCtr.numberOfPages = imgCnt;
    self.pageCtr = pageCtr;
    [self.view addSubview:pageCtr];
    //布局
    [pageCtr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view).mas_offset(-50);
    }];
}

//点击按钮
- (void)clickBtn:(UIButton*)btn{
    
    //用户偏好写入
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"key"];
    
    //跳转控制器
    LBTabBarController *tabBarCtr = [LBTabBarController new];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarCtr;

}

#pragma mark
#pragma mark - UIScrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = round(scrollView.contentOffset.x / screenSizeW);
    _pageCtr.currentPage = index;
}

@end
